package com.rw.coco.expression.parser;

import com.rw.coco.expression.parser.element.RfNumberElement;
import com.rw.tool.util.expression.element.ExpressionElement;
import com.rw.tool.util.expression.element.NumberElement;
import com.rw.tool.util.expression.expression.parser.element.ElementParser;
import com.rw.tool.util.string.StringScanner;

import java.util.Optional;
import java.util.regex.Pattern;

/**
 * 圣杯数字解析器
 */
public class RfNumberElementParser extends ElementParser<NumberElement> {
    private static final Pattern DICE_PATTERN = Pattern.compile(RfNumberElement.REGEX, Pattern.CASE_INSENSITIVE);

    public RfNumberElementParser() {
        super(NumberElement.class);
    }

    @Override
    public Optional<ExpressionElement> handler(StringScanner scanner, int scale) {
        // 如果当前状态能接受数字类型，则扫描数字类型
        if (scanner.hasNext(DICE_PATTERN)) {
            String next = scanner.next(DICE_PATTERN);
            return Optional.ofNullable(RfNumberElement.parse(next));
        }

        return Optional.empty();
    }
}
